package reviewAlgorithms.ListNode;

public class SplitListToParts {
    public static void main(String[] args) {

    }
    public ListNode[] splitListToParts(ListNode root, int k) {
        if (k < 1){
            return null;
        }
        ListNode[] res = new ListNode[k];
        int count = 0;
        ListNode currentNode = root;
        while (currentNode != null){
            count ++;
            currentNode = currentNode.next;
        }
        if (count < 1){
            return res;
        }
        int baseSize = count / k;
        int moreNode = count % k;
        currentNode = root;
        for (int i = 0; i < k; i++) {
            int size;
            if (i <= moreNode - 1){
                size = baseSize + 1;
            }else {
                size = baseSize;
            }
            res[i] = currentNode;
            if (currentNode == null){
                break;
            }
            for (int j = 0; j < size - 1; j++) {
                currentNode = currentNode.next;
            }
            ListNode temp = currentNode;
            currentNode = currentNode.next;
            temp.next = null;
        }
        return res;
    }
}
